package com.mobicartel.androidto;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.app.Activity;
import android.content.res.XmlResourceParser;
import android.util.Xml;

public class XmlPullFeedParser extends Activity {
	
	
	
    public XmlPullFeedParser() {
        super();
    }
    public List<Speaker> parse(String url) {
    	
        ArrayList<Speaker> messages = null;
        try {
        	URL speaker_xml_url = new URL(url);
        	
        	XmlPullParser parser = Xml.newPullParser();
            try {
                parser.setInput(speaker_xml_url.openConnection().getInputStream(), null);
            } finally {
            }

            int event_type = parser.getEventType();
        	Speaker current_speaker = null;
            boolean done = false;
            while (event_type != XmlResourceParser.END_DOCUMENT && !done){
                String name = null;
                switch (event_type){
                    case XmlResourceParser.START_DOCUMENT:
                        messages = new ArrayList<Speaker>();
                        break;
                    case XmlResourceParser.START_TAG:
                    	
                        name = parser.getName();
                        if (name.equalsIgnoreCase("speaker")){
                        	current_speaker = new Speaker();
                        } else if (current_speaker != null){
                            if (name.equals("name")){
                            	current_speaker.setName(parser.nextText());
                            } else if (name.equals("bio")){
                            	current_speaker.setBio(parser.nextText());
                            } 
                        }
                        break;
                    case XmlResourceParser.END_TAG:
                        name = parser.getName();
                        if (name.equalsIgnoreCase("speaker") && current_speaker != null){
                            messages.add(current_speaker);
                        } else if (name.equalsIgnoreCase("speakers_list")){
                            done = true;
                        }
                        break;
                }
                event_type = parser.next();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return messages;
    }
}